﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.BusinessCard;
using PetaPoco;

namespace BrnMall.Manager.Manager.BusinessCard
{
    /// <summary>
    /// 用户相册管理
    /// </summary>
    public static class TimeLineManager
    {
        public static Page<TimeLine> GetStoreUserTimeLine(int storeId, long pageNumber, long itemsPerPage)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Page<TimeLine>(pageNumber, itemsPerPage,
                    "select * from bc_TimeLine where storeid=@0 order by createtime desc", storeId);
            }
        }

        public static void InsertTimeLine(TimeLine timeLine)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(timeLine);
            }
        }

        public static Page<TimeLine> GetUserTimeLine(int uid, long pageNumber, long itemsPerPage)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Page<TimeLine>(pageNumber, itemsPerPage,
                    "select * from bc_TimeLine where uid=@0 order by createtime desc", uid);
            }
        }

        public static int DeleteTimeLine(int id)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("delete from bc_TimeLineImg where TId=@0", id);
                return db.Delete<TimeLine>(id);
            }
        }

        public static IList<TimeLineImg> GeTimeLineImgs(int tid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<TimeLineImg>("select * from bc_TimeLineImg where tid=@0 order by displayorder desc",
                    tid);
            }
        }

        public static IList<TimeLineImg> GetUserTimeLineImgs(int uid, int top = 1)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<TimeLineImg>(
                    $"select top {top} a.* from bc_TimeLineImg a left join bc_TimeLine b on a.tid=b.id where b.uid=@0 order by a.id,a.displayorder desc",uid);
            }
        }

        public static void InsertTimelineImg(TimeLineImg img)
        {
            using (var db = new QDMallDatabase())
            {

                db.Insert(img);

            }
        }
    }
}
